﻿namespace Jhong.FunMVCBlog.Core.Data
{
    using Entity;
    using Infrastructure;
    using Jhong.FunMVCBlog.Core.Enum;
    using System;
    using System.Collections.Generic;
    using Jhong.FunMVC.Razor;

    public class CategoryData : BaseData
    {
        public PageContainer<Category> GetList(int pageIndex, int itemsPerPage)
        {
            if (pageIndex < 1) pageIndex = 1;
            if (itemsPerPage < 1) itemsPerPage = 10;
            using (this._dbContext)
            {
                var items = this._dbContext.T("SELECT * FROM category WHERE IsDelete=@0 order by CreateTime Desc LIMIT @1,@2", DeleteStatus.No, (pageIndex - 1) * itemsPerPage, itemsPerPage).QueryToEntitys<Category>();
                var totalCount = this._dbContext.T("SELECT count(*) FROM category WHERE IsDelete=@0", DeleteStatus.No).QueryFirstOrDefault<int>();
                var container = new PageContainer<Category>(pageIndex, itemsPerPage, totalCount, items);
                return container;
            }
        }

        public Category GetByID(Guid id)
        {
            using (this._dbContext)
            {
                var entity = this._dbContext.T("SELECT * from category WHERE ID=@0", id).QueryFirstOrDefault<Category>();
                return entity;
            }
        }

        public IList<SelectListItem> GetAllCategorySelectList()
        {
            using (this._dbContext)
            {
                var data = this._dbContext.T("select * from category where IsDelete=@0", DeleteStatus.No).QueryToEntitys<Category>();
                var list = new List<SelectListItem>();
                foreach (var d in data)
                {
                    list.Add(new SelectListItem() { Value = d.ID.ToString(), Text = d.Name });
                }
                return list;
            }
        }

        private IList<Category> GetAll()
        {
            using (this._dbContext)
            {
                return this._dbContext.T("select * from category where IsShow=@0 ORDER BY CreateTime DESC", ShowStatus.Show).QueryToEntitys<Category>();
            }
        }

        private static IDictionary<Guid, string> _cache;
        private static readonly object _lock = new object();

        public IDictionary<Guid, string> Cache
        {
            get
            {
                lock (_lock)
                {
                    if (null == _cache)
                    {
                        var data = this.GetAll();
                        _cache = new Dictionary<Guid, string>();
                        foreach (var d in data)
                            _cache.Add(d.ID, d.Name);
                    }
                    return _cache;
                }
            }
        }

        public override bool Update<T>(T model)
        {
            lock (_lock)
            {
                _cache = null;
                using (this._dbContext)
                {
                    return this._dbContext.UpdateEntity(model) > 0;
                }
            }
        }

        public override bool Insert<T>(T model)
        {
            lock (_lock)
            {
                _cache = null;
                using (this._dbContext)
                {
                    return this._dbContext.InsertEntity(model) > 0;
                }
            }
        }

    }
}